import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:lcapp/api/interface/user_api.dart';
import 'package:lcapp/api/request/response_data.dart';
import 'package:lcapp/config/const_config.dart';
import 'package:lcapp/data/jy_address_entity.dart';
import 'package:lcapp/model/refresh/refresh.dart';
import 'package:lcapp/page/aMapView/amap_tool.dart';
import 'package:lcapp/widget/asr_app_bar.dart';

class MinAddressListPage extends StatefulWidget {
  final Map arguments;
  const MinAddressListPage({Key? key, required this.arguments}) : super(key: key);

  @override
  State<MinAddressListPage> createState() => _MinAddressListPageState();
}

class _MinAddressListPageState extends State<MinAddressListPage> {

  List<JyAddressEntity> _address = [];
  int page = 1;
  int size = 10;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getAdressList();
    WidgetsBinding.instance.addPostFrameCallback((callback)async {
      // AmapTool.startLocation(isShowToast: true);
    });
  }

  getAdressList()async{

    Map<String,dynamic> data = {
      'page':page,
      'size':size,
    };
    ResponseData res = await UserAPI.getMyJYaddressList(data);
    if(res.code==200){
      if(page==1){
        _address = [];
      }
      List arr = res.data??[];
      for (var element in arr) {
        JyAddressEntity entity = JyAddressEntity().fromJson(element);
        _address.add(entity);
      }
    }
    if(mounted){
      setState(() {});
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AsrAppBar(
        title: Text('经营地址',style: TextStyle(
          color: Colors.white,
          fontSize: 36.sp
        ),),
        backgroundColor: defaultBarColor,
        barColor: Colors.white,
      ),
      body: Container(
        child: Column(
          children: [
            Expanded(child:_address.length==0?Container(
              alignment: Alignment.center,
              child: Text('暂无经营地址快去添加吧~',style: TextStyle(
                color: Color(0xff999999),
                fontSize: 25.sp
              ),),
            ): EasyRefresh(
              header: myRefreshHeader(),
              footer: myRefreshFooter(),
              onRefresh: ()async{
                page=1;
                getAdressList();
              },
              onLoad: ()async{
                page++;
                getAdressList();
              },
              child: ListView.builder(
                padding: EdgeInsets.zero,
                itemBuilder: (context,index){
                  JyAddressEntity entity = _address[index];
                return GestureDetector(
                  onTap: (){
                    if(widget.arguments!=null&&widget.arguments['callBack']!=null){
                      widget.arguments['callBack'](entity);
                      Navigator.of(context).pop();
                    }else{
                      Navigator.of(context).pushNamed('MinAddAddressPage',arguments: {'address':entity.toJson()}).then((value){
                        page=1;
                        getAdressList();
                      });
                    }
                  },
                  child: Container(
                    padding: EdgeInsets.symmetric(vertical: 30.w),
                    decoration: BoxDecoration(
                        border: Border(bottom: BorderSide(width: 0.6,color: Color(0xffE9E8E9)))
                    ),
                    child: Container(
                      padding: EdgeInsets.symmetric(horizontal: 30.w),
                      child: Row(
                        crossAxisAlignment: CrossAxisAlignment.center,
                        children: [
                          Visibility(child: GestureDetector(
                            onTap: ()async{
                              entity.isDefault = '1';
                              ResponseData res = await UserAPI.updateMyJYaddress(entity.toJson());
                              if(res.code==200){
                                for (var element in _address) {
                                  if(entity.id!=element.id)
                                    element.isDefault = '0';
                                }
                                setState(() {});
                              }
                            },
                            child: Image.asset(entity.isDefault=='1'?'assets/home/address_sel.png': 'assets/home/address_unsel.png',width: 30.w,),
                          ),visible: widget.arguments==null || widget.arguments['callBack']==null,),
                          SizedBox(width: 30.w,),
                          Expanded(
                              child: Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: [
                                  Text('${entity.province} ${entity.city} ${entity.region}',style: TextStyle(
                                      color: Color(0xff444444),
                                      fontSize: 32.sp,
                                      fontWeight: FontWeight.w500
                                  ),),
                                  SizedBox(height: 8.w,),
                                  Text('${entity.detailAddress}',style: TextStyle(
                                      color: Color(0x8f444444),
                                      fontSize: 28.sp
                                  ),)
                                ],
                              )
                          ),
                          SizedBox(width: 30.w,),
                          GestureDetector(
                            onTap: (){
                              Navigator.of(context).pushNamed('MinAddAddressPage',arguments: {'address':entity.toJson()}).then((value){
                                page=1;
                                getAdressList();
                              });
                            },
                            child: Container(
                              child: Icon(Icons.edit,color: Color(0xff999999),size: 30.sp,),
                            ),
                          ),
                        ],
                      ),
                    ),
                  ),
                );
              },itemCount: _address.length,
              ),
            )),
            Container(
              margin: EdgeInsets.symmetric(vertical: 50.w),
              child: GestureDetector(
                onTap: (){
                  Navigator.of(context).pushNamed('MinAddAddressPage').then((value){
                    page=1;
                    getAdressList();
                  });
                },
                child: Container(
                  height: 100.w,
                  width: 600.w,
                  alignment: Alignment.center,
                  decoration: BoxDecoration(
                    color: defaultBarColor,
                    borderRadius: BorderRadius.circular(10.w)
                  ),
                  child: Text('添加经营地址',style: TextStyle(
                    color: Colors.white,
                    fontSize: 36.sp,
                    fontWeight: FontWeight.w600
                  ),),
                ),
              ),
            )
          ],
        ),
      ),
    );
  }
}
